﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFlatnessTest
{
    public class MiData
    {
        /// <summary>
        /// 最小相似度
        /// </summary>
        public static decimal MinSimi = 70;

        /// <summary>
        /// 是否为无效的值
        /// </summary>
        public bool IsNull { get; set; }
        /// <summary>
        /// 相似度
        /// </summary>
        public decimal Simi { get; set; }
        /// <summary>
        /// 最大值
        /// </summary>
        public decimal MaxVal { get; set; }
        /// <summary>
        /// 最小值
        /// </summary>
        public decimal MinVal { get; set; }
        /// <summary>
        /// 差值
        /// </summary>
        public decimal DiffVal { get; set; }
        /// <summary>
        /// 最大值（芯片）
        /// </summary>
        public decimal ChipMaxVal { get; set; }
        /// <summary>
        /// 最小值（芯片）
        /// </summary>
        public decimal ChipMinVal { get; set; }
        /// <summary>
        /// 差值（芯片）
        /// </summary>
        public decimal ChipDiffVal { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="txt">94.118,+00000.000,+00000.000</param>
        /// <returns></returns>
        public static MiData GetMiData(string txt)
        {
            MiData miData = new MiData();
            miData.IsNull = true;

            string info = txt.Trim();
            if (string.IsNullOrWhiteSpace(info))
                return miData;

            var data = info.Split(',');
            if (data.Length == 3)
            {
                try
                {
                    decimal d1 = Convert.ToDecimal(data[1]);
                    decimal d2 = Convert.ToDecimal(data[2]);

                    miData.MaxVal = Math.Max(d1, d2);
                    miData.MinVal = Math.Min(d1, d2);
                    miData.DiffVal = miData.MaxVal - miData.MinVal;

                    miData.Simi = Convert.ToDecimal(data[0]);
                    miData.IsNull = miData.Simi <= MiData.MinSimi;
                    return miData;

                }
                catch (Exception ex)
                {
                    miData.IsNull = true;
                    return miData;
                }
            }
            else if (data.Length == 5)
            {
                try
                {
                    decimal d1 = Convert.ToDecimal(data[1]);
                    decimal d2 = Convert.ToDecimal(data[2]);
                    decimal d3 = Convert.ToDecimal(data[3]);
                    decimal d4 = Convert.ToDecimal(data[4]);

                    miData.MaxVal = Math.Max(d1, d2);
                    miData.MinVal = Math.Min(d1, d2);
                    miData.DiffVal = miData.MaxVal - miData.MinVal;

                    miData.ChipMaxVal = Math.Max(d3, d4);
                    miData.ChipMinVal = Math.Min(d3, d4);
                    miData.ChipDiffVal = miData.ChipMaxVal - miData.ChipMinVal;

                    miData.Simi = Convert.ToDecimal(data[0]);
                    miData.IsNull = miData.Simi <= MiData.MinSimi;
                    return miData;

                }
                catch (Exception ex)
                {
                    miData.IsNull = true;
                    return miData;
                }
            }
            else
            {
                return miData;
            }
        }
    }
}
